package com.home.common;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class AbstractDaoSupport  extends HibernateDaoSupport {
	
	protected JdbcTemplate jdbcTemplate;
    
    private Session currentSession;
    

    public Session getCurrentSession() {
        return this.currentSession == null ? super.getSessionFactory().getCurrentSession() : null;
    }

    public void setCurrentSession(Session currentSession) {
        this.currentSession = currentSession;
    }

    public JdbcTemplate getJdbcTemplate() {
        if (jdbcTemplate == null) {
            throw new NoSuchBeanDefinitionException("jdbcTemplate", "No Spring JDBC Template Bean.");
        }
        return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
    public Query getSqlQuery(String queryString){
        return getCurrentSession().createSQLQuery(queryString);
    }
    
    public Query getHqlQuery(String queryString){
        return getCurrentSession().createQuery(queryString);
    }
}
